<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mock.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_mock_mock.js.html">mock.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides a mocking framework in Closure to make unit tests easy
<a name="line17"></a> * to write and understand. The methods provided here can be used to replace
<a name="line18"></a> * implementations of existing objects with &#39;mock&#39; objects to abstract out
<a name="line19"></a> * external services and dependencies thereby isolating the code under test.
<a name="line20"></a> * Apart from mocking, methods are also provided to just monitor calls to an
<a name="line21"></a> * object (spying) and returning specific values for some or all the inputs to
<a name="line22"></a> * methods (stubbing).
<a name="line23"></a> *
<a name="line24"></a> * Design doc : http://go/closuremock
<a name="line25"></a> *
<a name="line26"></a> */
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>goog.provide(&#39;goog.labs.mock&#39;);
<a name="line30"></a>
<a name="line31"></a>goog.require(&#39;goog.array&#39;);
<a name="line32"></a>goog.require(&#39;goog.debug.Error&#39;);
<a name="line33"></a>goog.require(&#39;goog.functions&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Mocks a given object or class.
<a name="line38"></a> *
<a name="line39"></a> * @param {!Object} objectOrClass An instance or a constructor of a class to be
<a name="line40"></a> *     mocked.
<a name="line41"></a> * @return {!Object} The mocked object.
<a name="line42"></a> */
<a name="line43"></a>goog.labs.mock = function(objectOrClass) {
<a name="line44"></a>  // Go over properties of &#39;objectOrClass&#39; and create a MockManager to
<a name="line45"></a>  // be used for stubbing out calls to methods.
<a name="line46"></a>  var mockObjectManager = new goog.labs.mock.MockObjectManager_(objectOrClass);
<a name="line47"></a>  var mockedObject = mockObjectManager.getMockedItem();
<a name="line48"></a>  goog.asserts.assertObject(mockedObject);
<a name="line49"></a>  return /** @type {!Object} */ (mockedObject);
<a name="line50"></a>};
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Mocks a given function.
<a name="line55"></a> *
<a name="line56"></a> * @param {!Function} func A function to be mocked.
<a name="line57"></a> * @return {!Function} The mocked function.
<a name="line58"></a> */
<a name="line59"></a>goog.labs.mockFunction = function(func) {
<a name="line60"></a>  var mockFuncManager = new goog.labs.mock.MockFunctionManager_(func);
<a name="line61"></a>  var mockedFunction = mockFuncManager.getMockedItem();
<a name="line62"></a>  goog.asserts.assertFunction(mockedFunction);
<a name="line63"></a>  return /** @type {!Function} */ (mockedFunction);
<a name="line64"></a>};
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Spies on a given object.
<a name="line69"></a> *
<a name="line70"></a> * @param {!Object} obj The object to be spied on.
<a name="line71"></a> * @return {!Object} The spy object.
<a name="line72"></a> */
<a name="line73"></a>goog.labs.mock.spy = function(obj) {
<a name="line74"></a>  // Go over properties of &#39;obj&#39; and create a MockSpyManager_ to
<a name="line75"></a>  // be used for spying on calls to methods.
<a name="line76"></a>  var mockSpyManager = new goog.labs.mock.MockSpyManager_(obj);
<a name="line77"></a>  var spyObject = mockSpyManager.getMockedItem();
<a name="line78"></a>  goog.asserts.assert(spyObject);
<a name="line79"></a>  return spyObject;
<a name="line80"></a>};
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>/**
<a name="line84"></a> * Returns an object that can be used to verify calls to specific methods of a
<a name="line85"></a> * given mock.
<a name="line86"></a> *
<a name="line87"></a> * @param {!Object} obj The mocked object.
<a name="line88"></a> * @return {!Object} The verifier.
<a name="line89"></a> */
<a name="line90"></a>goog.labs.mock.verify = function(obj) {
<a name="line91"></a>  return obj.$callVerifier;
<a name="line92"></a>};
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Error thrown when verification failed.
<a name="line98"></a> *
<a name="line99"></a> * @constructor
<a name="line100"></a> * @extends {goog.debug.Error}
<a name="line101"></a> */
<a name="line102"></a>goog.labs.mock.VerificationError = function() {
<a name="line103"></a>  goog.base(this, &#39;Verification failed!&#39;);
<a name="line104"></a>};
<a name="line105"></a>goog.inherits(goog.labs.mock.VerificationError, goog.debug.Error);
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * This array contains the name of the functions that are part of the base
<a name="line110"></a> * Object prototype.
<a name="line111"></a> * Basically a copy of goog.object.PROTOTYPE_FIELDS_.
<a name="line112"></a> * @const
<a name="line113"></a> * @type {!Array.&lt;string&gt;}
<a name="line114"></a> * @private
<a name="line115"></a> */
<a name="line116"></a>goog.labs.mock.PROTOTYPE_FIELDS_ = [
<a name="line117"></a>  &#39;constructor&#39;,
<a name="line118"></a>  &#39;hasOwnProperty&#39;,
<a name="line119"></a>  &#39;isPrototypeOf&#39;,
<a name="line120"></a>  &#39;propertyIsEnumerable&#39;,
<a name="line121"></a>  &#39;toLocaleString&#39;,
<a name="line122"></a>  &#39;toString&#39;,
<a name="line123"></a>  &#39;valueOf&#39;
<a name="line124"></a>];
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Base class that provides basic functionality for creating, adding and
<a name="line130"></a> * finding bindings, offering an executor method that is called when a call to
<a name="line131"></a> * the stub is made, an array to hold the bindings and the mocked item, among
<a name="line132"></a> * other things.
<a name="line133"></a> *
<a name="line134"></a> * @constructor
<a name="line135"></a> * @private
<a name="line136"></a> */
<a name="line137"></a>goog.labs.mock.MockManager_ = function() {
<a name="line138"></a>  /**
<a name="line139"></a>   * Proxies the methods for the mocked object or class to execute the stubs.
<a name="line140"></a>   * @type {!Object}
<a name="line141"></a>   * @protected
<a name="line142"></a>   * TODO(user): make instanceof work.
<a name="line143"></a>   */
<a name="line144"></a>  this.mockedItem = {};
<a name="line145"></a>
<a name="line146"></a>  /**
<a name="line147"></a>   * A reference to the object or function being mocked.
<a name="line148"></a>   * @type {Object|Function}
<a name="line149"></a>   * @protected
<a name="line150"></a>   */
<a name="line151"></a>  this.mockee = null;
<a name="line152"></a>
<a name="line153"></a>  /**
<a name="line154"></a>   * Holds the stub bindings established so far.
<a name="line155"></a>   * @protected
<a name="line156"></a>   */
<a name="line157"></a>  this.methodBindings = [];
<a name="line158"></a>
<a name="line159"></a>  /**
<a name="line160"></a>   * Holds a reference to the binder used to define stubs.
<a name="line161"></a>   * @protected
<a name="line162"></a>   */
<a name="line163"></a>  this.$stubBinder = null;
<a name="line164"></a>
<a name="line165"></a>  /**
<a name="line166"></a>   * Record method calls with no stub definitions.
<a name="line167"></a>   * @type {!Array.&lt;!goog.labs.mock.MethodBinding_&gt;}
<a name="line168"></a>   * @private
<a name="line169"></a>   */
<a name="line170"></a>  this.callRecords_ = [];
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Handles the first step in creating a stub, returning a stub-binder that
<a name="line176"></a> * is later used to bind a stub for a method.
<a name="line177"></a> *
<a name="line178"></a> * @param {string} methodName The name of the method being bound.
<a name="line179"></a> * @param {...} var_args The arguments to the method.
<a name="line180"></a> * @return {!goog.labs.mock.StubBinder_} The stub binder.
<a name="line181"></a> * @private
<a name="line182"></a> */
<a name="line183"></a>goog.labs.mock.MockManager_.prototype.handleMockCall_ =
<a name="line184"></a>    function(methodName, var_args) {
<a name="line185"></a>  var args = goog.array.slice(arguments, 1);
<a name="line186"></a>  return new goog.labs.mock.StubBinder_(this, methodName, args);
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Returns the mock object. This should have a stubbed method for each method
<a name="line192"></a> * on the object being mocked.
<a name="line193"></a> *
<a name="line194"></a> * @return {!Object|!Function} The mock object.
<a name="line195"></a> */
<a name="line196"></a>goog.labs.mock.MockManager_.prototype.getMockedItem = function() {
<a name="line197"></a>  return this.mockedItem;
<a name="line198"></a>};
<a name="line199"></a>
<a name="line200"></a>
<a name="line201"></a>/**
<a name="line202"></a> * Adds a binding for the method name and arguments to be stubbed.
<a name="line203"></a> *
<a name="line204"></a> * @param {?string} methodName The name of the stubbed method.
<a name="line205"></a> * @param {!Array} args The arguments passed to the method.
<a name="line206"></a> * @param {!Function} func The stub function.
<a name="line207"></a> *
<a name="line208"></a> */
<a name="line209"></a>goog.labs.mock.MockManager_.prototype.addBinding =
<a name="line210"></a>    function(methodName, args, func) {
<a name="line211"></a>  var binding = new goog.labs.mock.MethodBinding_(methodName, args, func);
<a name="line212"></a>  this.methodBindings.push(binding);
<a name="line213"></a>};
<a name="line214"></a>
<a name="line215"></a>
<a name="line216"></a>/**
<a name="line217"></a> * Returns a stub, if defined, for the method name and arguments passed in.
<a name="line218"></a> *
<a name="line219"></a> * @param {string} methodName The name of the stubbed method.
<a name="line220"></a> * @param {!Array} args The arguments passed to the method.
<a name="line221"></a> * @return {Function} The stub function or undefined.
<a name="line222"></a> * @protected
<a name="line223"></a> */
<a name="line224"></a>goog.labs.mock.MockManager_.prototype.findBinding =
<a name="line225"></a>    function(methodName, args) {
<a name="line226"></a>  var stub = goog.array.find(this.methodBindings, function(binding) {
<a name="line227"></a>    return binding.matches(methodName, args, false /* isVerification */);
<a name="line228"></a>  });
<a name="line229"></a>  return stub &amp;&amp; stub.getStub();
<a name="line230"></a>};
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * Returns a stub, if defined, for the method name and arguments passed in as
<a name="line235"></a> * parameters.
<a name="line236"></a> *
<a name="line237"></a> * @param {string} methodName The name of the stubbed method.
<a name="line238"></a> * @param {!Array} args The arguments passed to the method.
<a name="line239"></a> * @return {Function} The stub function or undefined.
<a name="line240"></a> * @protected
<a name="line241"></a> */
<a name="line242"></a>goog.labs.mock.MockManager_.prototype.getExecutor = function(methodName, args) {
<a name="line243"></a>  return this.findBinding(methodName, args);
<a name="line244"></a>};
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>/**
<a name="line248"></a> * Looks up the list of stubs defined on the mock object and executes the
<a name="line249"></a> * function associated with that stub.
<a name="line250"></a> *
<a name="line251"></a> * @param {string} methodName The name of the method to execute.
<a name="line252"></a> * @param {...} var_args The arguments passed to the method.
<a name="line253"></a> * @return {*} Value returned by the stub function.
<a name="line254"></a> * @protected
<a name="line255"></a> */
<a name="line256"></a>goog.labs.mock.MockManager_.prototype.executeStub =
<a name="line257"></a>    function(methodName, var_args) {
<a name="line258"></a>  var args = goog.array.slice(arguments, 1);
<a name="line259"></a>
<a name="line260"></a>  // Record this call
<a name="line261"></a>  this.recordCall_(methodName, args);
<a name="line262"></a>
<a name="line263"></a>  var func = this.getExecutor(methodName, args);
<a name="line264"></a>  if (func) {
<a name="line265"></a>    return func.apply(null, args);
<a name="line266"></a>  }
<a name="line267"></a>};
<a name="line268"></a>
<a name="line269"></a>
<a name="line270"></a>/**
<a name="line271"></a> * Records a call to &#39;methodName&#39; with arguments &#39;args&#39;.
<a name="line272"></a> *
<a name="line273"></a> * @param {string} methodName The name of the called method.
<a name="line274"></a> * @param {!Array} args The array of arguments.
<a name="line275"></a> * @private
<a name="line276"></a> */
<a name="line277"></a>goog.labs.mock.MockManager_.prototype.recordCall_ =
<a name="line278"></a>    function(methodName, args) {
<a name="line279"></a>  var callRecord = new goog.labs.mock.MethodBinding_(methodName, args,
<a name="line280"></a>      goog.nullFunction);
<a name="line281"></a>
<a name="line282"></a>  this.callRecords_.push(callRecord);
<a name="line283"></a>};
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * Verify invocation of a method with specific arguments.
<a name="line288"></a> *
<a name="line289"></a> * @param {string} methodName The name of the method.
<a name="line290"></a> * @param {...} var_args The arguments passed.
<a name="line291"></a> * @return {!Function} The stub, if defined, or the spy method.
<a name="line292"></a> * @protected
<a name="line293"></a> */
<a name="line294"></a>goog.labs.mock.MockManager_.prototype.verifyInvocation =
<a name="line295"></a>    function(methodName, var_args) {
<a name="line296"></a>  var args = goog.array.slice(arguments, 1);
<a name="line297"></a>  var binding = goog.array.find(this.callRecords_, function(binding) {
<a name="line298"></a>    return binding.matches(methodName, args, true /* isVerification */);
<a name="line299"></a>  });
<a name="line300"></a>
<a name="line301"></a>  if (!binding) {
<a name="line302"></a>    throw new goog.labs.mock.VerificationError();
<a name="line303"></a>  }
<a name="line304"></a>};
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>
<a name="line308"></a>/**
<a name="line309"></a> * Sets up mock for the given object (or class), stubbing out all the defined
<a name="line310"></a> * methods. By default, all stubs return {@code undefined}, though stubs can be
<a name="line311"></a> * later defined using {@code goog.labs.mock.when}.
<a name="line312"></a> *
<a name="line313"></a> * @param {!Object|!Function} objOrClass The object or class to set up the mock
<a name="line314"></a> *     for. A class is a constructor function.
<a name="line315"></a> *
<a name="line316"></a> * @constructor
<a name="line317"></a> * @extends {goog.labs.mock.MockManager_}
<a name="line318"></a> * @private
<a name="line319"></a> */
<a name="line320"></a>goog.labs.mock.MockObjectManager_ = function(objOrClass) {
<a name="line321"></a>  goog.base(this);
<a name="line322"></a>
<a name="line323"></a>  /**
<a name="line324"></a>   * Proxies the calls to establish the first step of the stub bindings (object
<a name="line325"></a>   * and method name)
<a name="line326"></a>   * @private
<a name="line327"></a>   */
<a name="line328"></a>  this.objectStubBinder_ = {};
<a name="line329"></a>
<a name="line330"></a>  this.mockee = objOrClass;
<a name="line331"></a>
<a name="line332"></a>  /**
<a name="line333"></a>   * The call verifier is used to verify the calls. It maps property names to
<a name="line334"></a>   * the method that does call verification.
<a name="line335"></a>   * @type {!Object.&lt;string, function(string, ...)&gt;}
<a name="line336"></a>   * @private
<a name="line337"></a>   */
<a name="line338"></a>  this.objectCallVerifier_ = {};
<a name="line339"></a>
<a name="line340"></a>  var obj;
<a name="line341"></a>  if (goog.isFunction(objOrClass)) {
<a name="line342"></a>    // Create a temporary subclass with a no-op constructor so that we can
<a name="line343"></a>    // create an instance and determine what methods it has.
<a name="line344"></a>    /** @constructor */
<a name="line345"></a>    var tempCtor = function() {};
<a name="line346"></a>    goog.inherits(tempCtor, objOrClass);
<a name="line347"></a>    obj = new tempCtor();
<a name="line348"></a>  } else {
<a name="line349"></a>    obj = objOrClass;
<a name="line350"></a>  }
<a name="line351"></a>
<a name="line352"></a>  var enumerableProperties = goog.object.getKeys(obj);
<a name="line353"></a>  // The non enumerable properties are added due to the fact that IE8 does not
<a name="line354"></a>  // enumerate any of the prototype Object functions even when overriden and
<a name="line355"></a>  // mocking these is sometimes needed.
<a name="line356"></a>  for (var i = 0; i &lt; goog.labs.mock.PROTOTYPE_FIELDS_.length; i++) {
<a name="line357"></a>    var prop = goog.labs.mock.PROTOTYPE_FIELDS_[i];
<a name="line358"></a>    if (!goog.array.contains(enumerableProperties, prop)) {
<a name="line359"></a>      enumerableProperties.push(prop);
<a name="line360"></a>    }
<a name="line361"></a>  }
<a name="line362"></a>
<a name="line363"></a>  // Adds the properties to the mock, creating a proxy stub for each method on
<a name="line364"></a>  // the instance.
<a name="line365"></a>  for (var i = 0; i &lt; enumerableProperties.length; i++) {
<a name="line366"></a>    var prop = enumerableProperties[i];
<a name="line367"></a>    if (goog.isFunction(obj[prop])) {
<a name="line368"></a>      this.mockedItem[prop] = goog.bind(this.executeStub, this, prop);
<a name="line369"></a>      // The stub binder used to create bindings.
<a name="line370"></a>      this.objectStubBinder_[prop] =
<a name="line371"></a>          goog.bind(this.handleMockCall_, this, prop);
<a name="line372"></a>      // The verifier verifies the calls.
<a name="line373"></a>      this.objectCallVerifier_[prop] =
<a name="line374"></a>          goog.bind(this.verifyInvocation, this, prop);
<a name="line375"></a>    }
<a name="line376"></a>  }
<a name="line377"></a>  // The alias for stub binder exposed to the world.
<a name="line378"></a>  this.mockedItem.$stubBinder = this.objectStubBinder_;
<a name="line379"></a>
<a name="line380"></a>  // The alias for verifier for the world.
<a name="line381"></a>  this.mockedItem.$callVerifier = this.objectCallVerifier_;
<a name="line382"></a>};
<a name="line383"></a>goog.inherits(goog.labs.mock.MockObjectManager_,
<a name="line384"></a>              goog.labs.mock.MockManager_);
<a name="line385"></a>
<a name="line386"></a>
<a name="line387"></a>
<a name="line388"></a>/**
<a name="line389"></a> * Sets up the spying behavior for the given object.
<a name="line390"></a> *
<a name="line391"></a> * @param {!Object} obj The object to be spied on.
<a name="line392"></a> *
<a name="line393"></a> * @constructor
<a name="line394"></a> * @extends {goog.labs.mock.MockObjectManager_}
<a name="line395"></a> * @private
<a name="line396"></a> */
<a name="line397"></a>goog.labs.mock.MockSpyManager_ = function(obj) {
<a name="line398"></a>  goog.base(this, obj);
<a name="line399"></a>};
<a name="line400"></a>goog.inherits(goog.labs.mock.MockSpyManager_,
<a name="line401"></a>              goog.labs.mock.MockObjectManager_);
<a name="line402"></a>
<a name="line403"></a>
<a name="line404"></a>/**
<a name="line405"></a> * Return a stub, if defined, for the method and arguments passed in. If we lack
<a name="line406"></a> * a stub, instead look for a call record that matches the method and arguments.
<a name="line407"></a> *
<a name="line408"></a> * @return {Function} The stub or the invocation logger, if defined.
<a name="line409"></a> * @override
<a name="line410"></a> */
<a name="line411"></a>goog.labs.mock.MockSpyManager_.prototype.findBinding =
<a name="line412"></a>    function(methodName, args) {
<a name="line413"></a>  var stub = goog.base(this, &#39;findBinding&#39;, methodName, args);
<a name="line414"></a>
<a name="line415"></a>  if (!stub) {
<a name="line416"></a>    stub = goog.bind(this.mockee[methodName], this.mockee);
<a name="line417"></a>  }
<a name="line418"></a>
<a name="line419"></a>  return stub;
<a name="line420"></a>};
<a name="line421"></a>
<a name="line422"></a>
<a name="line423"></a>
<a name="line424"></a>/**
<a name="line425"></a> * Sets up mock for the given function, stubbing out. By default, all stubs
<a name="line426"></a> * return {@code undefined}, though stubs can be later defined using
<a name="line427"></a> * {@code goog.labs.mock.when}.
<a name="line428"></a> *
<a name="line429"></a> * @param {!Function} func The function to set up the mock for.
<a name="line430"></a> *
<a name="line431"></a> * @constructor
<a name="line432"></a> * @extends {goog.labs.mock.MockManager_}
<a name="line433"></a> * @private
<a name="line434"></a> */
<a name="line435"></a>goog.labs.mock.MockFunctionManager_ = function(func) {
<a name="line436"></a>  goog.base(this);
<a name="line437"></a>
<a name="line438"></a>  /**
<a name="line439"></a>   * The stub binder used to create bindings.
<a name="line440"></a>   * @type {!Function}
<a name="line441"></a>   * @private
<a name="line442"></a>   */
<a name="line443"></a>  this.functionStubBinder_ = goog.bind(this.handleMockCall_, this, null);
<a name="line444"></a>
<a name="line445"></a>  this.mockedItem = goog.bind(this.executeStub, this, null);
<a name="line446"></a>  this.mockedItem.$stubBinder = this.functionStubBinder_;
<a name="line447"></a>
<a name="line448"></a>  /**
<a name="line449"></a>   * The call verifier is used to verify function invocations.
<a name="line450"></a>   * @type {!Function}
<a name="line451"></a>   */
<a name="line452"></a>  this.mockedItem.$callVerifier = goog.bind(this.verifyInvocation, this, null);
<a name="line453"></a>};
<a name="line454"></a>goog.inherits(goog.labs.mock.MockFunctionManager_,
<a name="line455"></a>              goog.labs.mock.MockManager_);
<a name="line456"></a>
<a name="line457"></a>
<a name="line458"></a>
<a name="line459"></a>/**
<a name="line460"></a> * The stub binder is the object that helps define the stubs by binding
<a name="line461"></a> * method name to the stub method.
<a name="line462"></a> *
<a name="line463"></a> * @param {!goog.labs.mock.MockManager_}
<a name="line464"></a> *   mockManager The mock manager.
<a name="line465"></a> * @param {?string} name The method name.
<a name="line466"></a> * @param {!Array} args The other arguments to the method.
<a name="line467"></a> *
<a name="line468"></a> * @constructor
<a name="line469"></a> * @private
<a name="line470"></a> */
<a name="line471"></a>goog.labs.mock.StubBinder_ = function(mockManager, name, args) {
<a name="line472"></a>  /**
<a name="line473"></a>   * The mock manager instance.
<a name="line474"></a>   * @type {!goog.labs.mock.MockManager_}
<a name="line475"></a>   * @private
<a name="line476"></a>   */
<a name="line477"></a>  this.mockManager_ = mockManager;
<a name="line478"></a>
<a name="line479"></a>  /**
<a name="line480"></a>   * Holds the name of the method to be bound.
<a name="line481"></a>   * @type {?string}
<a name="line482"></a>   * @private
<a name="line483"></a>   */
<a name="line484"></a>  this.name_ = name;
<a name="line485"></a>
<a name="line486"></a>  /**
<a name="line487"></a>   * Holds the arguments for the method.
<a name="line488"></a>   * @type {!Array}
<a name="line489"></a>   * @private
<a name="line490"></a>   */
<a name="line491"></a>  this.args_ = args;
<a name="line492"></a>};
<a name="line493"></a>
<a name="line494"></a>
<a name="line495"></a>/**
<a name="line496"></a> * Defines the stub to be called for the method name and arguments bound
<a name="line497"></a> * earlier.
<a name="line498"></a> * TODO(user): Add support for the &#39;Answer&#39; interface.
<a name="line499"></a> *
<a name="line500"></a> * @param {!Function} func The stub.
<a name="line501"></a> */
<a name="line502"></a>goog.labs.mock.StubBinder_.prototype.then = function(func) {
<a name="line503"></a>  this.mockManager_.addBinding(this.name_, this.args_, func);
<a name="line504"></a>};
<a name="line505"></a>
<a name="line506"></a>
<a name="line507"></a>/**
<a name="line508"></a> * Defines the stub to return a specific value for a method name and arguments.
<a name="line509"></a> *
<a name="line510"></a> * @param {*} value The value to return.
<a name="line511"></a> */
<a name="line512"></a>goog.labs.mock.StubBinder_.prototype.thenReturn = function(value) {
<a name="line513"></a>  this.mockManager_.addBinding(this.name_, this.args_,
<a name="line514"></a>                               goog.functions.constant(value));
<a name="line515"></a>};
<a name="line516"></a>
<a name="line517"></a>
<a name="line518"></a>/**
<a name="line519"></a> * Facilitates (and is the first step in) setting up stubs. Obtains an object
<a name="line520"></a> * on which, the method to be mocked is called to create a stub. Sample usage:
<a name="line521"></a> *
<a name="line522"></a> * var mockObj = goog.labs.mock(objectBeingMocked);
<a name="line523"></a> * goog.labs.mock.when(mockObj).getFoo(3).thenReturn(4);
<a name="line524"></a> *
<a name="line525"></a> * @param {!Object} mockObject The mocked object.
<a name="line526"></a> * @return {!goog.labs.mock.StubBinder_} The property binder.
<a name="line527"></a> */
<a name="line528"></a>goog.labs.mock.when = function(mockObject) {
<a name="line529"></a>  goog.asserts.assert(mockObject.$stubBinder, &#39;Stub binder cannot be null!&#39;);
<a name="line530"></a>  return mockObject.$stubBinder;
<a name="line531"></a>};
<a name="line532"></a>
<a name="line533"></a>
<a name="line534"></a>
<a name="line535"></a>/**
<a name="line536"></a> * Represents a binding between a method name, args and a stub.
<a name="line537"></a> *
<a name="line538"></a> * @param {?string} methodName The name of the method being stubbed.
<a name="line539"></a> * @param {!Array} args The arguments passed to the method.
<a name="line540"></a> * @param {!Function} stub The stub function to be called for the given method.
<a name="line541"></a> * @constructor
<a name="line542"></a> * @private
<a name="line543"></a> */
<a name="line544"></a>goog.labs.mock.MethodBinding_ = function(methodName, args, stub) {
<a name="line545"></a>  /**
<a name="line546"></a>   * The name of the method being stubbed.
<a name="line547"></a>   * @type {?string}
<a name="line548"></a>   * @private
<a name="line549"></a>   */
<a name="line550"></a>  this.methodName_ = methodName;
<a name="line551"></a>
<a name="line552"></a>  /**
<a name="line553"></a>   * The arguments for the method being stubbed.
<a name="line554"></a>   * @type {!Array}
<a name="line555"></a>   * @private
<a name="line556"></a>   */
<a name="line557"></a>  this.args_ = args;
<a name="line558"></a>
<a name="line559"></a>  /**
<a name="line560"></a>   * The stub function.
<a name="line561"></a>   * @type {!Function}
<a name="line562"></a>   * @private
<a name="line563"></a>   */
<a name="line564"></a>  this.stub_ = stub;
<a name="line565"></a>};
<a name="line566"></a>
<a name="line567"></a>
<a name="line568"></a>/**
<a name="line569"></a> * @return {!Function} The stub to be executed.
<a name="line570"></a> */
<a name="line571"></a>goog.labs.mock.MethodBinding_.prototype.getStub = function() {
<a name="line572"></a>  return this.stub_;
<a name="line573"></a>};
<a name="line574"></a>
<a name="line575"></a>
<a name="line576"></a>/**
<a name="line577"></a> * Determines whether the given args match the stored args_. Used to determine
<a name="line578"></a> * which stub to invoke for a method.
<a name="line579"></a> *
<a name="line580"></a> * @param {string} methodName The name of the method being stubbed.
<a name="line581"></a> * @param {!Array} args An array of arguments.
<a name="line582"></a> * @param {boolean} isVerification Whether this is a function verification call
<a name="line583"></a> *     or not.
<a name="line584"></a> * @return {boolean} If it matches the stored arguments.
<a name="line585"></a> */
<a name="line586"></a>goog.labs.mock.MethodBinding_.prototype.matches = function(
<a name="line587"></a>    methodName, args, isVerification) {
<a name="line588"></a>  var specs = isVerification ? args : this.args_;
<a name="line589"></a>  var calls = isVerification ? this.args_ : args;
<a name="line590"></a>
<a name="line591"></a>  //TODO(user): More elaborate argument matching. Think about matching
<a name="line592"></a>  //    objects.
<a name="line593"></a>  return this.methodName_ == methodName &amp;&amp;
<a name="line594"></a>      goog.array.equals(calls, specs, function(arg, spec) {
<a name="line595"></a>        // Duck-type to see if this is an object that implements the
<a name="line596"></a>        // goog.labs.testing.Matcher interface.
<a name="line597"></a>        if (goog.isFunction(spec.matches)) {
<a name="line598"></a>          return spec.matches(arg);
<a name="line599"></a>        } else {
<a name="line600"></a>          return goog.array.defaultCompareEquality(spec, arg);
<a name="line601"></a>        }
<a name="line602"></a>      });
<a name="line603"></a>};
</pre>


</body>
</html>
